package com.esd.device.controller;

import com.esd.common.pojo.R;
import com.esd.device.entity.EsdLogDetail;
import com.esd.device.entity.dto.EsdLogSearch;
import com.esd.device.service.EsdLogDetailService;
import com.github.liaochong.myexcel.core.DefaultExcelBuilder;
import com.github.liaochong.myexcel.utils.AttachmentExportUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/esdLogDetail")
public class EsdLogDetailController {

    @Autowired
    private EsdLogDetailService esdLogDetailService ;

    @PostMapping("logSearch")
    public R logSearch(
            @RequestBody EsdLogSearch esdLogSearch
    ){
        return esdLogDetailService.logSearch(esdLogSearch) ;
    }

    @PostMapping("exportData")
    public void exportData(
            @RequestBody EsdLogSearch esdLogSearch ,
            HttpServletResponse response) throws IOException {
        List<EsdLogDetail> esdLogDetails = esdLogDetailService.exportData(esdLogSearch);
        Workbook workbook = DefaultExcelBuilder.of(EsdLogDetail.class).build(esdLogDetails);
        AttachmentExportUtil.export(workbook, "data", response);
    }
}
